package com.ruicar.afs.cloud.channel.common.mq.sender.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsRelTemp;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsTemp;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsRelTempService;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsTempService;
import com.ruicar.afs.cloud.channel.common.condition.CaseAffiliationStatusCondition;
import com.ruicar.afs.cloud.channel.common.enmus.ChannelAffiiationEnum;
import com.ruicar.afs.cloud.channel.common.mq.sender.CaseAffiliationRelSender;
import com.ruicar.afs.cloud.channel.common.mq.sender.CaseAffiliationRelStatusSender;
import com.ruicar.afs.cloud.channel.common.mq.sender.CaseAffiliationStatusSender;
import com.ruicar.afs.cloud.channel.common.mq.sender.impl.CaseAffiliationRelSenderImpl;
import com.ruicar.afs.cloud.channel.common.mq.sender.impl.CaseAffiliationRelStatusSenderImpl;
import com.ruicar.afs.cloud.channel.common.mq.sender.impl.CaseAffiliationStatusSenderImpl;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelInfo;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationRelStatusDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.affiliation.AffiliationStatusInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author panhj
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/caseAffiliation")
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class CaseAffiliationStatusController {

    private final CaseAffiliationStatusSender sender;
    private final CaseAffiliationRelSender caseAffiliationRelSender;
    private final CaseAffiliationRelStatusSender caseAffiliationRelStatusSender;
    private final CaseAffiliationStatusSenderImpl caseAffiliationStatusSenderService;
    private final CaseAffiliationRelSenderImpl caseAffiliationRelSenderService;
    private final CaseAffiliationRelStatusSenderImpl caseAffiliationRelStatusService;
    private final ChannelAffiliatedUnitsTempService channelAffiliatedUnitsTempService;
    private final ChannelAffiliatedUnitsRelTempService channelAffiliatedUnitsRelTempService;

    /**
     * 提交案件挂靠-发送消息到消费者
     *
     * @return
     */
    @PostMapping("/updateCaseAffiliationStatus")
    @ApiOperation("提交案件挂靠状态")
    public IResponse updateCaseAffiliationStatus(@RequestBody CaseAffiliationStatusCondition condition) {
        AfsTransEntity<AffiliationStatusInfo> transEntity = this.caseAffiliationStatusSenderService.returnAffiliationStatusInfo(condition.getId());
        sender.sendForCaseAffiliationStatus(transEntity);
        return IResponse.success("消息发送成功");
    }

    /**
     * 提交案件挂靠-发送消息到消费者
     *
     * @return
     */
    @PostMapping("/pushToCaseAffiliation")
    @ApiOperation("提交案件挂靠及合作商")
    public IResponse pushToCaseAffiliation(@RequestBody CaseAffiliationStatusCondition condition) {
        AfsTransEntity<AffiliationRelInfo> transEntity = this.caseAffiliationRelSenderService.returnAffiliationRelInfo(condition.getId());
        log.info("挂靠及合作商信息……", transEntity);
        caseAffiliationRelSender.pushInfoToCaeAffiliation(transEntity);
        return IResponse.success("消息发送成功");
    }

    /**
     * 提交案件挂靠-发送消息到消费者
     *
     * @return
     */
    @PostMapping("/pushToCaseAffiliationRel")
    @ApiOperation("提交案件挂靠及合作商")
    public IResponse pushToCaseAffiliationRel(@RequestBody CaseAffiliationStatusCondition condition) {
        AfsTransEntity<AffiliationRelStatusDto> transEntity = this.caseAffiliationRelStatusService.returnAffiliationRelInfo(condition.getId());
        log.info("挂靠及合作商信息……", transEntity);
        caseAffiliationRelStatusSender.sendForCaseRelAffiliationStatus(transEntity);
        return IResponse.success("消息发送成功");
    }

    @RequestMapping(value = "/affiliatedCount/{id}", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("进件申请挂靠数量计算")
    public IResponse affiliatedCount(@PathVariable String id) {
        ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getById(id);
        if (channelAffiliatedUnitsTemp != null) {
            if(channelAffiliatedUnitsTemp.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())){
                if (channelAffiliatedUnitsTemp.getMonthlySurplusNum() == 0 || channelAffiliatedUnitsTemp.getTotalSurplusNum() == 0) {
                    return IResponse.fail("该挂靠公司申请数量已用尽");
                }
                channelAffiliatedUnitsTemp.setMonthlySurplusNum(channelAffiliatedUnitsTemp.getMonthlySurplusNum() - 1);
                channelAffiliatedUnitsTemp.setTotalSurplusNum(channelAffiliatedUnitsTemp.getTotalSurplusNum() - 1);
                if (channelAffiliatedUnitsTemp.getMonthlySurplusNum() == 0 || channelAffiliatedUnitsTemp.getTotalSurplusNum() == 0) {
                    if ((ChannelAffiiationEnum.START.getCode()).equals(channelAffiliatedUnitsTemp.getStatus())) {
                        channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.STOP.getCode());
                        channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                    }
                    channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                    List<ChannelAffiliatedUnitsRelTemp> channelAffiliatedUnitsRelTempList = channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                            .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channelAffiliatedUnitsTemp.getId()).notIn(ChannelAffiliatedUnitsRelTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
                    for (int i = 0; i < channelAffiliatedUnitsRelTempList.size(); i++) {
                        channelAffiliatedUnitsRelTempList.get(i).setStatus(ChannelAffiiationEnum.STOP.getCode());
                        channelAffiliatedUnitsRelTempList.get(i).setUpdateTime(new Date());
                        channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTempList.get(i));
                    }
                }else{
                    channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                }
            }
            AfsTransEntity<AffiliationRelInfo> transEntity = this.caseAffiliationRelSenderService.returnAffiliationRelInfo(id);
            log.info("挂靠及合作商信息……", transEntity);
            caseAffiliationRelSender.pushInfoToCaeAffiliation(transEntity);
            return IResponse.success("申请成功");
        } else {
            return IResponse.fail("未获取到挂靠信息");
        }
    }

    @PostMapping("/stopChannel")
    @ApiOperation(value = "停用合作商与挂靠公司关系")
    public IResponse stopChannel(@RequestBody CaseAffiliationStatusCondition condition) {
        String[] idArray = condition.getIds().split(",");
        String id = "";
        for (int j = 0; j < idArray.length; j++) {
            Long channel=Long.valueOf(idArray[j]);
            ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                    .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel).eq(ChannelAffiliatedUnitsRelTemp::getChannelId, condition.getChannelId()));
            if (channelAffiliatedUnitsRelTemp != null) {
                channelAffiliatedUnitsRelTemp.setStatus(condition.getStatus());
                channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                id=channelAffiliatedUnitsRelTemp.getId().toString()+",";
            }
        }
        AfsTransEntity<AffiliationRelStatusDto> transEntity = this.caseAffiliationRelStatusService.returnAffiliationRelInfo(id);
        log.info("挂靠及合作商信息……", transEntity);
        caseAffiliationRelStatusSender.sendForCaseRelAffiliationStatus(transEntity);
        return IResponse.success("操作成功");
    }
}
